//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package requirement_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_requirement_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type FourQInfoDbItem struct {
	RequirementId            string `bson:"_id"`
	UrgentAndImportant       uint32 `bson:"urgentAndImportant"`
	UrgentAndNotImportant    uint32 `bson:"urgentAndNotImportant"`
	NotUrgentAndNotImportant uint32 `bson:"notUrgentAndNotImportant"`
	NotUrgentAndImportant    uint32 `bson:"notUrgentAndImportant"`
}

func (item *FourQInfoDbItem) ToFourQInfo() *project_requirement_api.FourQInfo {
	return &project_requirement_api.FourQInfo{
		RequirementId:            item.RequirementId,
		UrgentAndImportant:       item.UrgentAndImportant,
		UrgentAndNotImportant:    item.UrgentAndNotImportant,
		NotUrgentAndNotImportant: item.NotUrgentAndNotImportant,
		NotUrgentAndImportant:    item.NotUrgentAndImportant,
	}
}

type _FourQInfoDao struct {
	mc *mongo.Collection
}

func newFourQInfoDao(db *mongo.Database) (*_FourQInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_REQUIRE_MENT + "fourq_info")
	return &_FourQInfoDao{mc: mc}, nil
}

func (db *_FourQInfoDao) Set(ctx context.Context, dbItem *FourQInfoDbItem) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.RequirementId}, bson.M{"$set": dbItem},
		options.Update().SetUpsert(true))
	return err
}

func (db *_FourQInfoDao) Get(ctx context.Context, requirementId string) (*FourQInfoDbItem, error) {
	retItem := &FourQInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": requirementId}).Decode(retItem)
	return retItem, err
}

func (db *_FourQInfoDao) Remove(ctx context.Context, requirementId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": requirementId})
	return err
}
